package com.devinsor.demo.action;

import com.devinsor.demo.service.IModifyService;
import com.devinsor.demo.service.IQueryService;
import com.devinsor.spring.framework.annotation.Autowried;
import com.devinsor.spring.framework.annotation.Controller;
import com.devinsor.spring.framework.annotation.RequestMapping;
import com.devinsor.spring.framework.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: com.elvis.demo-v1
 * @description: 公布接口url
 * @author: JackRen
 * @create: 2020-05-13 14:22
 **/
@Controller
@RequestMapping("/web")
public class DemoAction {

        @Autowried
        IQueryService queryService;
        @Autowried
        IModifyService modifyService;

        @RequestMapping("/query.json")
        public void query(HttpServletRequest request, HttpServletResponse response,
                          @RequestParam("name") String name){
            String result = queryService.query(name);
            out(response,result);
        }

        @RequestMapping("/add*.json")
        public void add(HttpServletRequest request,HttpServletResponse response,
                        @RequestParam("name") String name,@RequestParam("addr") String addr){
            String result = modifyService.add(name,addr);
            out(response,result);
        }

        @RequestMapping("/remove.json")
        public void remove(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("id") Integer id){
            String result = modifyService.remove(id);
            out(response,result);
        }

        @RequestMapping("/edit.json")
        public void edit(HttpServletRequest request,HttpServletResponse response,
                         @RequestParam("id") Integer id,
                         @RequestParam("name") String name){
            String result = modifyService.edit(id,name);
            out(response,result);
        }



        private void out(HttpServletResponse resp,String str){
            try {
                resp.getWriter().write(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}
